import request from '@/utils/request'
import type {
  skuInfoData,
  SkuData,
  SpuData,
  HasSpuResponseData,
  AllTradeMark,
  SpuHasImg,
  SaleAttrResponseData,
  HasSaleAttrResponseData,
} from './type'

enum API {
  HAS_SPU_URL = '/admin/product',
  // 获取全部品牌的数据
  ALL_TRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  // 获取某个SPU下的全部的售卖商品的图片数据
  IMAGE_URL = '/admin/product/spuImageList',
  // 获取某个SPU下所有的销售属性列表
  SPU_HAS_SALE_ATTR_URL = '/admin/product/spuSaleAttrList',
  // 获取整个项目全部的销售属性
  ALL_SALE_ATTR_URL = '/admin/product/baseSaleAttrList',
  // 新增SPU接口
  ADD_SPU_URL = '/admin/product/saveSpuInfo',
  // 修改SPU接口
  UPDATE_SPU_URL = '/admin/product/updateSpuInfo',
  // 追加一个新的SKU
  ADD_SKU_URL = '/admin/product/saveSkuInfo',
  // 查看某一个已有的SPU下全部售卖的商品
  SKU_INFO_URL = '/admin/product/findBySpuId',
  // 删除已有的SPU
  REMOVE_SPU_URL = '/admin/product/deleteSpu',
}

export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: string | number,
) =>
  request.get<any, HasSpuResponseData>(
    `${API.HAS_SPU_URL}/${page}/${limit}?category3Id=${category3Id}`,
  )

// 获取全部的SPU的品牌的数据
export const reqAllTradeMark = () =>
  request.get<any, AllTradeMark>(API.ALL_TRADEMARK_URL)
export const reqSpuImageList = (spuId: number) =>
  request.get<any, SpuHasImg>(`${API.IMAGE_URL}/${spuId}`)
export const reqSpuHasSaleAttr = (spuId: number) =>
  request.get<any, SaleAttrResponseData>(
    `${API.SPU_HAS_SALE_ATTR_URL}/${spuId}`,
  )
export const reqAllSaleAttr = () =>
  request.get<any, HasSaleAttrResponseData>(API.ALL_SALE_ATTR_URL)

export const reqAddOrUpdateSpu = (data: SpuData) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATE_SPU_URL, data)
  } else {
    return request.post<any, any>(API.ADD_SPU_URL, data)
  }
}

export const reqAddSku = (data: SkuData) =>
  request.post<any, any>(API.ADD_SKU_URL, data)

export const reqSkuList = (spuId: number | string) =>
  request.get<any, skuInfoData>(`${API.SKU_INFO_URL}/${spuId}`)

export const reqRemoveSpu = (spuId: number | string) =>
  request.delete<any, any>(`${API.REMOVE_SPU_URL}/${spuId}`)
